import Vue from 'vue'
import store from '../store'
import Router from 'vue-router'
 
import index from '@/view/index/index'
import login from '@/view/login/login'


  
   
Vue.use(Router)

const router = new Router({
    routes: [
      {
        path: '/',
        redirect: '/index',
        meta: {
        requiresAuth: true,
        },
      },
      {
        path: '/login',
        component: login ,
   
      },
      {
        path: '/index',
        component: index ,
        meta: {
        requiresAuth: true,
        },
      },
    ]
  })
  router.beforeEach((to, from, next) => {
    let token = store.state.token;
    if(to.meta.requiresAuth){
      if(token){
        next();
      }else{
        next({
          path: '/login',
          query: { redirect: to.fullPath }  // 将刚刚要去的路由path（却无权限）作为参数，方便登录成功后直接跳转到该路由
        });
      }
    }else{
      next();//如果无需token 
    }
  });


  export default  router;
